package com.wonderful.eyepetizer.ui.pages.discover

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import coil.compose.rememberAsyncImagePainter
import com.wonderful.eyepetizer.R
import com.wonderful.eyepetizer.app.ktx.loadMoreView
import com.wonderful.eyepetizer.data.entity.bean.Item
import com.wonderful.eyepetizer.ui.pages.detail.VideoDetailActivity
import com.wonderful.eyepetizer.ui.theme.Black_26
import com.wonderful.eyepetizer.ui.theme.Follow_bg
import com.wonderful.eyepetizer.ui.theme.White_54
import com.wonderful.eyepetizer.utils.DateUtils

/**
 * Desc: 发现-关注
 * @Author: wonderful
 * @Time: 2024/2/21 16:37
 */

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun FollowPage(viewModel: DiscoverViewModel = viewModel()) {
    val lazyPagingItems = viewModel.followPageFlow.collectAsLazyPagingItems()
    val pullRefreshState = rememberPullRefreshState(
        refreshing = viewModel.followRefreshing.value,
        onRefresh = { lazyPagingItems.refresh() }
    )
    Box(
        modifier = Modifier
            .fillMaxSize()
            .pullRefresh(pullRefreshState)
    ) {
        LazyColumn(modifier = Modifier.fillMaxSize(), contentPadding = PaddingValues(top = 10.dp)) {
            items(lazyPagingItems.itemCount) {
                val item = lazyPagingItems[it]
                FollowItemWidget(item = item!!)
            }
            loadMoreView(lazyPagingItems)
        }
        PullRefreshIndicator(
            refreshing = viewModel.followRefreshing.value,
            state = pullRefreshState,
            modifier = Modifier.align(Alignment.TopCenter)
        )
    }
}

@Composable
fun FollowItemWidget(item: Item) {
    Column(
        modifier = Modifier
            .padding(start = 15.dp, top = 2.dp, bottom = 15.dp)
            .fillMaxWidth()
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 10.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painter = rememberAsyncImagePainter(model = item.data?.header?.icon),
                contentDescription = null,
                modifier = Modifier
                    .size(40.dp)
                    .clip(CircleShape)
            )
            Column(
                modifier = Modifier
                    .weight(1f)
                    .padding(start = 10.dp)
            ) {
                Text(
                    text = item.data?.header?.title ?: "",
                    style = TextStyle(color = Color.Black, fontSize = 14.sp)
                )
                Text(
                    text = item.data?.header?.description ?: "",
                    style = TextStyle(color = Black_26, fontSize = 14.sp),
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier.padding(top = 3.dp)
                )
            }
            Text(
                text = stringResource(id = R.string.add_follow),
                style = TextStyle(color = Color.Gray, fontSize = 14.sp),
                modifier = Modifier
                    .padding(end = 10.dp)
                    .clip(RoundedCornerShape(5.dp))
                    .background(Follow_bg)
                    .padding(5.dp)
            )
        }
        item.data?.itemList?.run {
            LazyRow {
                items(item.data.itemList) {
                    FollowHorizontalItemWidget(item = it)
                }
            }
        }
        Divider(thickness = 0.5.dp, modifier = Modifier.padding(top = 10.dp))
    }
}

@Composable
fun FollowHorizontalItemWidget(item: Item) {
    val context = LocalContext.current
    Column(modifier = Modifier
        .padding(end = 15.dp)
        .clickable {
            item.data?.run {
                VideoDetailActivity.actionStart(context, item.data)
            }
        }) {
        Box {
            Image(
                painter = rememberAsyncImagePainter(model = item.data?.cover?.feed),
                contentDescription = null,
                modifier = Modifier
                    .width(300.dp)
                    .height(180.dp)
                    .clip(RoundedCornerShape(4.dp)),
                contentScale = ContentScale.Crop
            )
            Text(
                text = item.data?.category ?: "",
                style = TextStyle(
                    fontSize = 13.sp,
                    color = Color.Black,
                    fontWeight = FontWeight.Bold
                ),
                modifier = Modifier
                    .padding(top = 8.dp, end = 8.dp)
                    .clip(RoundedCornerShape(5.dp))
                    .background(White_54)
                    .padding(6.dp)
                    .align(Alignment.TopEnd)
            )
        }
        Text(
            text = item.data?.title ?: "",
            style = TextStyle(fontSize = 14.sp, color = Color.Black, fontWeight = FontWeight.Bold),
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            modifier = Modifier.padding(top = 3.dp)
        )
        Text(
            text = DateUtils.formatDateMsByYMDHM(item.data?.author?.latestReleaseTime ?: 0),
            style = TextStyle(fontSize = 12.sp, color = Black_26),
            modifier = Modifier.padding(top = 3.dp)
        )
    }
}

